﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Controls;

namespace SHomeWorkshop.LunarSF.StudentInfosManager.ValidationRules
{
    /// <summary>
    /// 邮件验证类。
    /// </summary>
    public class EmailRule : LValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            string strValue = Format(value.ToString());

            if (string.IsNullOrWhiteSpace(strValue))
                return new ValidationResult(true, null); //电子邮箱地址可以为空。验证OK。

            string expression = @"^[_a-z\d\-\./]+@[_a-z\d\-]+(\.[_a-z\d\-]+)*(\.(info|biz|com|edu|gov|net|am|bz|cn|cx|hk|jp|tw|vc|vn))$";//@"^([\w_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
            if (Regex.IsMatch(strValue, expression, RegexOptions.Compiled))
                return new ValidationResult(true, null); //验证OK
            else
                return new ValidationResult(false, "“电子邮件”地址形如：abc@xxx.xx\r\n只支持如下后缀：\r\ninfo|biz|com|edu|gov|net|am|bz|cn|cx|hk|jp|tw|vc|vn"); //验证失败
        }

        public static string Format(string origin)
        {
            if (string.IsNullOrWhiteSpace(origin)) return "";

            return FullCharsToHalf(origin);
        }
    }
}
